module.exports = {
	/**
	 * 添加单条数据
	 * @url admin/user/sys/add 前端调用的url参数地址
	 * data 请求参数 说明
	 * res 返回参数说明
	 * @params {Number} code 错误码，0表示成功
	 * @params {String} msg 详细信息
	 */
	main: async (event) => {
		const db = uniCloud.database();
		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			_
		} = util;
		let {
			uid
		} = data;
		let res = {
			code: 0,
			msg: 'ok'
		};
		// 业务逻辑开始-----------------------------------------------------------
		// 获取前端传过来的参数
		let {
			username,
			password,
			password_secret_version,
			nickname,
			gender = 0,
			status = 0,
			mobile,
			mobile_confirmed = 0,
			email,
			email_confirmed = 0,
			avatar,
			avatar_file,
			department_id,
			role,
			wx_unionid,
			platform,
			tt_openid,
			ks_openid,
			wx_openid,
			dcloud_appid,
			comment,
			third_party,
			register_env,
			realname_auth,
			openid,
			zj_openid,
			had_youhui,
			plus_type,
			plus_expire_time,
			plus_expire_time_str,
			myinviter,
			balance,
			appid,
			appname,
			remark,
			remark1,
			remark2,
			remark3,
			score,
			register_date,
			register_ip,
			last_login_date,
			last_login_ip,
			token,
			invite_code,
			inviter_uid,
			finish,
			money,
			agent,
			agent_name,
			agent_id,
			allow_login_background,
			login_appid_type,
			invite_time,
			my_invite_code,
			middleman_id,
			middleman_username,
			middleman_nickname,
			wx_user_id,
			commission_rate
		} = data;
		// 参数验证开始

		/**
		 * 判断数据库是否已存在该用户名
		 */
		let check_username = await db.collection('uni-id-users').where({
			username: username
		}).count()
		if (check_username.total > 0) {
			res = {
				code: 1,
				msg: "已存在此用户名"
			};
			return res;
		}

		// 参数验证结束
		let dbName = "uni-id-users"; // 表名

		if (!middleman_id || !middleman_username || !middleman_nickname) {
			middleman_id = userInfo._id;
			middleman_nickname = userInfo.nickname;
			middleman_username = userInfo.username;
		}

		if (middleman_id) {
			let middleman_info = await db.collection('uni-id-users').doc(middleman_id).get()
			if (commission_rate > middleman_info.data[0].commission_rate) {
				return {
					code: -1,
					msg: "代理商佣金比例不能高于机构的佣金比例"
				}
			}
		}

		if (commission_rate > userInfo.commission_rate || commission_rate < 0) {
			return {
				code: -1,
				msg: "代理商佣金比例不能高于机构的佣金比例"
			}
		}
		// 执行 数据库add 命令
		res.id = await vk.baseDao.add({
			dbName,
			dataJson: {
				username,
				password,
				password_secret_version,
				nickname,
				gender,
				status,
				mobile,
				mobile_confirmed,
				email,
				email_confirmed,
				avatar,
				avatar_file,
				department_id,
				role,
				wx_unionid,
				platform,
				tt_openid,
				ks_openid,
				wx_openid,
				dcloud_appid,
				comment,
				third_party,
				register_env,
				realname_auth,
				openid,
				zj_openid,
				had_youhui,
				plus_type,
				plus_expire_time,
				plus_expire_time_str,
				myinviter,
				balance,
				appid,
				appname,
				remark,
				remark1,
				remark2,
				remark3,
				score,
				register_date,
				register_ip,
				last_login_date,
				last_login_ip,
				token,
				invite_code,
				inviter_uid,
				finish,
				money,
				agent,
				agent_name,
				agent_id,
				allow_login_background,
				login_appid_type,
				invite_time,
				my_invite_code,
				middleman_id,
				middleman_username,
				middleman_nickname,
				wx_user_id,
				commission_rate
			}
		});

		//如果绑定微信用户，就把微信用户的my_invite_code参数改成代理商的_ID
		if (wx_user_id) {
			let userinfo = await db.collection('uni-id-users').doc(wx_user_id).get()
			if (userinfo.data[0].my_invite_code.length > 20) {
				return { code: -1, msg: '绑定失败！该微信账号已绑定代理商，代理商ID:' + userinfo.data[0].my_invite_code }
			}

			await vk.baseDao.updateById({
				dbName,
				id: wx_user_id,
				dataJson: {
					my_invite_code: res.id
				}
			});
		}
		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}
